package com.wenjie.controller.sys;

import com.mybatisflex.core.query.QueryWrapper;
import com.wenjie.original.entity.SysMenu;
import com.wenjie.original.entity.SysRoleMenu;
import com.wenjie.original.entity.common.vo.RestBaseVo;
import com.wenjie.original.entity.table.SysMenuTableDef;
import com.wenjie.original.entity.table.SysRoleMenuTableDef;
import com.wenjie.original.entity.vo.SysRole.SysRoleUpOrAddVo;
import com.wenjie.original.entity.vo.SysRole.SysRoleVo;
import com.wenjie.original.entity.vo.SysRoleMenu.SysRoleMenuVo;
import com.wenjie.original.service.ISysMenuService;
import com.wenjie.original.service.ISysRoleMenuService;
import com.wenjie.original.service.ISysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@Api("角色管理")
@RestController
@RequestMapping("/api/sysRole")
public class SysRoleController {

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private ISysRoleMenuService sysRoleMenuService;

    @Autowired
    private ISysMenuService sysMenuService;


    @ApiOperation("查询接口")
    @PostMapping("/list")
    public RestBaseVo<SysRoleVo.ResVo> list(@RequestBody @Validated SysRoleVo.SelVo selVo) {
        return sysRoleService.list(selVo, SysRoleVo.ResVo.class);
    }


    @ApiOperation("查询角色的菜单")
    @PostMapping("/listByMenuRole")
    public RestBaseVo<SysRoleMenuVo.ResVo> listByMenuRole(@RequestBody @Validated SysRoleMenuVo.SelVo selVo) {
        return sysRoleMenuService.list(selVo, SysRoleMenuVo.ResVo.class);
    }


    @ApiOperation("查询角色的菜单")
    @PostMapping("/listByMenu")
    public List<ISysMenuService.Menu> listByMenu(@RequestBody @Validated String sysRoleId) {
        List<SysRoleMenu> list = sysRoleMenuService.list(QueryWrapper.create().where(SysRoleMenuTableDef.SYS_ROLE_MENU.SYS_ROLE_ID.eq(sysRoleId)));
        List<Long> menuIds = list.stream().map(s -> s.getSysMenuId()).collect(Collectors.toList());
        List<SysMenu> list1 = sysMenuService.list(QueryWrapper.create().where(SysMenuTableDef.SYS_MENU.SYS_MENU_ID.in(menuIds)));
        return sysMenuService.treeStructure(list1); // 将数据转换为树形结构

    }

    @ApiOperation("添加接口")
    @PostMapping("/add")
    public void add(@RequestBody @Validated SysRoleUpOrAddVo.AddVo addVo) {
        sysRoleService.cusSave(addVo);
    }

    @ApiOperation("修改接口")
    @PostMapping("/up")
    public void up(@RequestBody @Validated SysRoleUpOrAddVo.UpVo upVo) {
        sysRoleService.cusUpdate(upVo);
    }

    @ApiOperation("删除接口")
    @PostMapping("/del")
    public void del(@RequestBody @Validated Long id) {
        sysRoleService.cueDel(id);
    }


}
